package com.hhkj.api.order.common.component.processor;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.hhkj.admin.order.domain.OrderRefund;
import com.hhkj.admin.order.model.req.OrderRefundReq;
import com.hhkj.admin.order.service.base.OrderRefundService;
import com.hhkj.admin.order.service.biz.OrderRefundBizService;
import com.hhkj.admin.order.service.extend.OrderRefundExtendService;
import com.hhkj.payment.wechat.notification.common.processor.RefundSuccessProcessor;
import com.ruoyi.common.exception.GlobalException;
import com.wechat.pay.java.service.refund.model.RefundNotification;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * ApiOrderWechatPayRefundSuccessProcessor
 *
 * @author zheng
 * @since 2023/11/22 22:51
 */
@Slf4j
@Component
public class ApiOrderWechatPayRefundSuccessProcessor implements RefundSuccessProcessor {

    @Autowired
    private OrderRefundExtendService orderRefundExtendService;
    @Autowired
    private OrderRefundService orderRefundService;
    @Autowired
    private OrderRefundBizService orderRefundBizService;

    @Override
    public void process(RefundNotification refundNotification) {
        if (ObjectUtil.isEmpty(refundNotification)) {
            throw new GlobalException("信息读取失败！");
        }
        log.info(refundNotification.toString());
        OrderRefund refundPo
                = orderRefundExtendService.getByNum(refundNotification.getOutRefundNo());
        if (ObjectUtil.isEmpty(refundPo)) {
            throw new GlobalException("退款信息不存在！");
        }
        OrderRefundReq orderRefundReq = new OrderRefundReq();
        orderRefundReq.setOrderId(refundPo.getOrderId());
        orderRefundReq.setFinalPrice(refundPo.getPrice());
        orderRefundReq.setPayoutDate(DateUtil.parse(refundNotification.getSuccessTime()));
        orderRefundBizService.payout(orderRefundReq, refundPo.getCreateBy());
    }
}
